package com.loper7.base.ui;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.WindowManager;

import com.loper7.base.R;
import com.loper7.base.utils.AppManager;
import com.loper7.base.utils.BackHelper;
import com.loper7.base.utils.DensityUtil;
import com.loper7.base.utils.KeyboardUtil;
import com.loper7.base.utils.StatusBarHelper;
import com.loper7.base.utils.UIHelper;
import com.loper7.base.widget.dialog.SweetAlertDialog;

import butterknife.ButterKnife;

/**
 * 应用程序Activity的基类
 *
 * @author
 * @version 1.0
 * @created 2012-9-18
 */

public abstract class BaseActivity extends AppCompatActivity {
    public Context context;
    public BaseActivity activity;
    public BackHelper backHelper;
    public SweetAlertDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        loadingDialog = new SweetAlertDialog(this);
        loadingDialog.setCancelable(true);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    loadingDialog.cancel();
                    if (onDialogBackPressed())
                        backHelper.backward();

                }
                return false;
            }
        });

        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        AppManager.getAppManager().addActivity(this);

        context = this;
        activity = this;
        backHelper = new BackHelper(this);

        //屏幕适配
        //DensityUtil.setDefault(this);

        if (getContentViewId() != 0) {
            setContentView(getContentViewId());
        }

        initData();
        initView();
        initListener();
    }

    protected abstract int getContentViewId();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract void initListener();

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
        setStatusBar();
    }

    /**
     * 设置状态栏
     */
    protected void setStatusBar() {
        StatusBarHelper.setStatusBar(this, ContextCompat.getColor(this, R.color.colorPrimary));
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //关闭软键盘
        KeyboardUtil.closeKeyboard(this);
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }

    /**
     * 是否相应dialog的返回按钮监听
     *
     * @return true：dialog显示状态按下返回键直接退出activity  false：dialog显示状态按下返回键cancel掉当前dialog
     */
    protected boolean onDialogBackPressed() {
        return true;
    }


    public void showLoading() {
        if (loadingDialog != null && !loadingDialog.isShowing())
            loadingDialog.show();
    }

    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

    @Override
    public void onStop() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
        super.onStop();
    }

    public void showMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeToast(this, message).show();
    }

    public void showSuccessMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeSuccessToast(this, message).show();
    }

    public void showErrorMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeErrorToast(this, message).show();
    }

    public void showWarningMessage(String message) {
        if (!TextUtils.isEmpty(message))
            UIHelper.makeWarringToast(this, message).show();
    }

}